library(ggplot2)
library(patchwork)

# set to desired working directory
setwd("4_IAT")

# Run Models
source("20_IAT_regs_fullsample.R")
source("20_IAT_regs_blackwhite.R")
source("20_IAT_regs_ideowhite.R")

# Start log file for script -----------------------------------------------
TeachingDemos::txtStart("./log files/figureA17.txt")

# Individual Plots --------------------------------------------------------
p_full <- ggplot(coef_out_Fsamp, aes(x = coef, y = var)) +
  geom_vline(xintercept = 0, linetype = "solid", color = "red") +
  geom_point(aes(color = indx, shape = indx, size = indx)) +
  scale_shape_manual(values = c(16, 18), name = "") +
  geom_linerange(aes(xmin = coef - 1.96*se, 
                     xmax = coef + 1.96*se,
                     color = indx),
                 lwd = .5) +
  scale_color_manual(values = c("black", "grey"), name = "") +
  scale_size_manual(values = c(2, 3), name = "") +
  theme_bw() +
  labs(y = "", x = "", title = "Full Sample") +
  scale_x_continuous(labels = numform::ff_num(zero = 0, digits = 2),
                     limits = c(-.17,.17)) +
  theme(legend.position = "bottom",
        plot.title = element_text(size = 16, hjust = 0.5),
        axis.title = element_text(size = 12),
        axis.text = element_text(size = 10),
        panel.grid.major.x = element_blank(), 
        panel.grid.minor = element_blank(),
        legend.text = element_text(size = 14),
        strip.text = element_text(size = 16),
        strip.background = element_blank())

p_wht <- ggplot(subset(coef_out_bw, race == "Whites"), aes(x = coef, y = var)) +
  geom_vline(xintercept = 0, linetype = "solid", color = "red") +
  geom_point(aes(color = indx, shape = indx, size = indx)) +
  scale_shape_manual(values = c(16, 18), name = "") +
  geom_linerange(aes(xmin = coef - 1.96*se, 
                     xmax = coef + 1.96*se,
                     color = indx),
                 lwd = .5) +
  scale_color_manual(values = c("black", "grey"), name = "") +
  scale_size_manual(values = c(2, 3), name = "") +
  theme_bw() +
  labs(y = "", x = "",  title = "Whites") +
  scale_x_continuous(labels = numform::ff_num(zero = 0, digits = 2),
                     limits = c(-.17,.17)) +
  theme(legend.position = "bottom",
        plot.title = element_text(size = 16, hjust = 0.5),
        axis.title = element_text(size = 12),
        axis.text = element_text(size = 10),
        panel.grid.major.x = element_blank(), 
        panel.grid.minor = element_blank(),
        legend.text = element_text(size = 14),
        strip.text = element_text(size = 16),
        strip.background = element_blank())

p_blk <- ggplot(subset(coef_out_bw, race == "Blacks"), aes(x = coef, y = var)) +
  geom_vline(xintercept = 0, linetype = "solid", color = "red") +
  geom_point(aes(color = indx, shape = indx, size = indx)) +
  scale_shape_manual(values = c(16, 18), name = "") +
  geom_linerange(aes(xmin = coef - 1.96*se, 
                     xmax = coef + 1.96*se,
                     color = indx),
                 lwd = .5) +
  scale_color_manual(values = c("black", "grey"), name = "") +
  scale_size_manual(values = c(2, 3), name = "") +
  theme_bw() +
  labs(y = "", x = "",  title = "Blacks") +
  scale_x_continuous(labels = numform::ff_num(zero = 0, digits = 2),
                     limits = c(-.17,.17)) +
  theme(legend.position = "bottom",
        plot.title = element_text(size = 16, hjust = 0.5),
        axis.title = element_text(size = 12),
        axis.text = element_text(size = 10),
        panel.grid.major.x = element_blank(), 
        panel.grid.minor = element_blank(),
        legend.text = element_text(size = 14),
        strip.text = element_text(size = 16),
        strip.background = element_blank())

p_con <- ggplot(subset(coef_out_ideo, ideo == "White Conservatives"), aes(x = coef, y = var)) +
  geom_vline(xintercept = 0, linetype = "solid", color = "red") +
  geom_point(aes(color = indx, shape = indx, size = indx)) +
  scale_shape_manual(values = c(16, 18), name = "") +
  geom_linerange(aes(xmin = coef - 1.96*se, 
                     xmax = coef + 1.96*se,
                     color = indx),
                 lwd = .5) +
  scale_color_manual(values = c("black", "grey"), name = "") +
  scale_size_manual(values = c(2, 3), name = "") +
  theme_bw() +
  labs(y = "", x = "", title = "White\nConservatives") +
  scale_x_continuous(labels = numform::ff_num(zero = 0, digits = 2),
                     limits = c(-.17,.17)) +
  theme(legend.position = "bottom",
        plot.title = element_text(size = 16, hjust = 0.5),
        axis.title = element_text(size = 12),
        axis.text = element_text(size = 10),
        panel.grid.major.x = element_blank(), 
        panel.grid.minor = element_blank(),
        legend.text = element_text(size = 14),
        strip.text = element_text(size = 16),
        strip.background = element_blank())

p_mod <- ggplot(subset(coef_out_ideo, ideo == "White Neutrals\n(Moderates)"), aes(x = coef, y = var)) +
  geom_vline(xintercept = 0, linetype = "solid", color = "red") +
  geom_point(aes(color = indx, shape = indx, size = indx)) +
  scale_shape_manual(values = c(16, 18), name = "") +
  geom_linerange(aes(xmin = coef - 1.96*se, 
                     xmax = coef + 1.96*se,
                     color = indx),
                 lwd = .5) +
  scale_color_manual(values = c("black", "grey"), name = "") +
  scale_size_manual(values = c(2, 3), name = "") +
  theme_bw() +
  labs(y = "", x = "", title = "White Neutrals\n(Moderates)") +
  scale_x_continuous(labels = numform::ff_num(zero = 0, digits = 2),
                     limits = c(-.17,.17)) +
  theme(legend.position = "bottom",
        plot.title = element_text(size = 16, hjust = 0.5),
        axis.title = element_text(size = 12),
        axis.text = element_text(size = 10),
        panel.grid.major.x = element_blank(), 
        panel.grid.minor = element_blank(),
        legend.text = element_text(size = 14),
        strip.text = element_text(size = 16),
        strip.background = element_blank())

p_lib <- ggplot(subset(coef_out_ideo, ideo == "White Liberals"), aes(x = coef, y = var)) +
  geom_vline(xintercept = 0, linetype = "solid", color = "red") +
  geom_point(aes(color = indx, shape = indx, size = indx)) +
  scale_shape_manual(values = c(16, 18), name = "") +
  geom_linerange(aes(xmin = coef - 1.96*se, 
                     xmax = coef + 1.96*se,
                     color = indx),
                 lwd = .5) +
  scale_color_manual(values = c("black", "grey"), name = "") +
  scale_size_manual(values = c(2, 3), name = "") +
  theme_bw() +
  labs(y = "", x = "", title = "White\nLiberals") +
  scale_x_continuous(labels = numform::ff_num(zero = 0, digits = 2),
                     limits = c(-.17,.17)) +
  theme(legend.position = "bottom",
        plot.title = element_text(size = 16, hjust = 0.5),
        axis.title = element_text(size = 12),
        axis.text = element_text(size = 10),
        panel.grid.major.x = element_blank(), 
        panel.grid.minor = element_blank(),
        legend.text = element_text(size = 14),
        strip.text = element_text(size = 16),
        strip.background = element_blank())

# Combine -----------------------------------------------------------------

p_full + p_wht + p_blk + p_con + p_mod + p_lib +
  plot_layout(guides = 'collect', nrow = 2) &
  theme(plot.tag = element_text(size = 18),
        plot.caption = element_text(size = 14, hjust = 0),
        strip.text = element_text(size = 16),
        axis.title = element_text(size = 16),
        legend.text = element_text(size = 16),
        legend.position = "bottom")
ggsave("./figures/FigureA17.pdf", width = 14, height = 10)

# End log file for script -------------------------------------------------
TeachingDemos::txtStop()